﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CtefCrmImport.Common.Models;
using Microsoft.Xrm.Sdk;
using CtefCrmImport.Common.Enums;

namespace CtefCrmImport.Common.Extensions
{
    public static class EntityExtensions
    {
        public static Donation ToDonation(this Entity e)
        {
            if (e == null)
            {
                return null;
            }

            return new Donation
            {
                Date = e.GetAttributeValue<DateTime>("new_date"),
                GiftAmount = e.GetAttributeValue<Money>("new_amount").Value,
                CompanyMatch = e.GetAttributeValue<Money>("new_microsoftmatchamount").Value,
                OtherInfo = e.GetAttributeValue<string>("new_additionalinfo"),
                FundType = (DonationType)e.GetAttributeValue<OptionSetValue>("new_type").Value
            };
        }

        public static bool Matches(this Donation d1, Donation d2)
        {
            if (d1 == null || d2 == null)
            {
                return d1 == d2;
            }

            return 
                d1.Date.Year == d2.Date.Year && 
                d1.Date.DayOfYear == d2.Date.DayOfYear && 
                d1.GiftAmount == d2.GiftAmount && 
                d1.CompanyMatch == d2.CompanyMatch;
        }
    }
}
